

//通用弹出式文件上传
function commonUpload(callback){
    $.dialog({
           content: "url:systemController.do?commonUpload",
           lock : true,
           title:"文件上传",
           zIndex:2100,
           width:700,
           height: 200,
           parent:windowapi,
           cache:false,
       ok: function(){
               var iframe = this.iframe.contentWindow;
               iframe.uploadCallback(callback);
                   return true;
       },
       cancelVal: '关闭',
       cancel: function(){
       } 
   });
}
function browseImages(inputId, Img) {// 图片管理器，可多个上传共用
}
function browseFiles(inputId, file) {// 文件管理器，可多个上传共用
}
function decode(value, id) {//value传入值,id接受值
	var last = value.lastIndexOf("/");
	var filename = value.substring(last + 1, value.length);
	$("#" + id).text(decodeURIComponent(filename));
}


//跳转添加问题页面
function openAddQuestions(){
	//添加一个问题模板
	 var tr =  $("#addQuestion_div tr").clone();
 	 $("#add_jformEmployeeMealsCost_table").append(tr);
 	// resetTrNum('add_jformEmployeeMealsCost_table');
 	 index = index +1;
 	//resetTrNum(tableId)
 	 resetTrNum('add_jformEmployeeMealsCost_table');
 	 return false;
    //  createwindow("问卷问题添加", "testQuestionController.do?goAdd","800px","400px");
}

function openAddSelects(){
	 var tr =  $("#addSelects_div tr").clone();
	 $("#add_selects_table").append(tr);
	 selectIndex = selectIndex+1;
	 return false;
}


//初始化下标
function resetTrNum(tableId) {
	$tbody = $("#"+tableId+"");
	$tbody.find('>tr').each(function(i){
		$(':input, select,button,a', this).each(function(){
			var $this = $(this), name = $this.attr('name'),id=$this.attr('id'),onclick_str=$this.attr('onclick'), val = $this.val();
			if(name!=null){
				if (name.indexOf("#index#") >= 0){
					$this.attr("name",name.replace('#index#',i));
				}else{
					var s = name.indexOf("[");
					var e = name.indexOf("]");
					var new_name = name.substring(s+1,e);
					$this.attr("name",name.replace(new_name,i));
				}
			}
			if(id!=null){
				if (id.indexOf("#index#") >= 0){
					$this.attr("id",id.replace('#index#',i));
				}else{
					var s = id.indexOf("[");
					var e = id.indexOf("]");
					var new_id = id.substring(s+1,e);
					$this.attr("id",id.replace(new_id,i));
				}
			}
			if(onclick_str!=null){
				if (onclick_str.indexOf("#index#") >= 0){
					$this.attr("onclick",onclick_str.replace(/#index#/g,i));
				}else{
				}
			}
		});
		$(this).find('label[name=\'xh\']').html(index);
	});
}


var no =1;

function addQue(){
	var data = {};
	data.no = no++;
	data.que = '请填写问题内容';
	_.templateSettings = {
		    interpolate: /\<\@\=(.+?)\@\>/gim,
		    evaluate: /\<\@(.+?)\@\>/gim,
		    escape: /\<\@\-(.+?)\@\>/gim
		};
	var $content = $(_.template($("#expPanelTemp").html())({
		data : data
	}));
	
	
	$($content.find('div.addOpt')).click(function(){
		var optdata = {};
		optdata.no = $(this).parent().find('.opt-div .opt').length + 1;
		optdata.que = '请填写选项内容';
		var $optContent = $(_.template($("#optTemp").html())({
			data : optdata
		}));
		$content.find('div.opt-div').append($optContent);
	});
	
	$('#que-div').append($content);
}

//提交数据
function commitDate(){
	var bpmTitle = $("#bpmTitle").val();
	var by3 = $("#by3").val();
	 var title;
	 var questions = new Array();
	//获取 问题的div
	var quediv = $('.que');
//	$(".que").each(function(i){
//		var selects = new Array();
//		//问题内容描述
//		var queRe = $(".que .questionAs #q")[i].value;
//		//问题类型
//		var queType = $(".que #type #questionType")[i].value;
//		$(".que .questionAs .opt-div  ").each(function(j){ 
//			//选项描述内容
//			var questionRe = $(".que .questionAs .opt-div  .selectRe #q")[j].value
//			//选项答案
//			var questionAs = $(".que .questionAs .opt-div .as #questionAs")[j].value;
//			selects.push(new select(questionRe,questionAs));
//			questions.push(new question(queRe,queType,selects));
//			console.log(j)
//			});
//		
//		$('.div').each(function(j){
//		})
//	})
	 var questions = new Array();
	$(".que").each(function(index, que){
		var selects = new Array();
		//问题内容描述
		var queRe = $(que).find("input[name='qu']").val();
		//问题类型
		var queType = $(que).find("#type #questionType").val();
		$(que).find(".opt-div .opt").each(function(optIndex, opt){ 
			//选项描述内容
			var o = {};
			o.questionRe = $(opt).find("input[name='qo']").val();
			//选项答案
			o.questionAs = $(opt).find("select[name='questionAs']").val();
			selects.push(o);
			var q = {};
			q.queRe = queRe;
			q.queType = queType;
			q.selects = selects;
			questions.push(q);
		});
	})
	
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		data : {questions:JSON.stringify(questions),title:title,by3:by3,bpmTitle:bpmTitle},
		url : "questionConfigController.do?commitdate",    // 请求的action路径
		error : function() {// 请求失败处理函数
		},
		success : function(data) {
			console.log( data);
			var d = $.parseJSON(data);
			console.log(d);
			console.log(d.success);
			if (d.success) {
				var msg = d.msg;
				console.log(msg);
				//表头
				 title = d.title;
				//问题数组
				console.log(title);
				 questions = d.questions;
				console.log(d.questions);
				tip(msg);
			} else {
				tip(msg);
			}
		}
	});
	
	
}